//
//  PhotoViewController.m
//  Clock
//
//  Created by Zc on 16/4/10.
//  Copyright © 2016年 qianfeng. All rights reserved.
//

#import "PhotoViewController.h"
#import "UIImageView+WebCache.h"
#import "MyModel.h"
#import "MyPicCell.h"

@interface PhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray * sortArray;

@property (nonatomic, strong) NSMutableArray * dataArray;

@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"欣赏";
    _sortArray = [[NSMutableArray alloc] init];
    _dataArray = [[NSMutableArray alloc] init];
    self.view.backgroundColor = [UIColor yellowColor];
    [self createUI];
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNav];
}

- (void)initData{
    [HttpTool getWithURL:SORT_URL params:nil success:^(id json) {
        if ([json[@"res"] integerValue] == 0) {
            NSArray * sortArray = json[@"data"];
            for (int i = 0; i < [sortArray count]; i++) {
                [_sortArray addObject:[sortArray objectAtIndex:i]];
            }
            [_collectionView reloadData];
        }else{
            [MBProgressHUD showError:@"好像有什么地方不对呢"];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络好像出错了呢"];
    }];
}

- (void)createUI{
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpNav{
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]};
    /**
     * 添加左侧返回按钮
     */
    
    self.navigationController.navigationBar.translucent = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem.tintColor=[UIColor blackColor];
}

-(void)back
{
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"rippleEffect";
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:0.3];
}

#pragma mark - UICollectionViewDelegate & DataSource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyPicCell * cell = [MyPicCell cellWithCollectionView:collectionView andIndexPath:indexPath];
    
    cell.contentView.backgroundColor = [self randomColor];
    
    [HttpTool getWithURL:[DETAIL_URL stringByAppendingString:[_sortArray objectAtIndex:indexPath.row]] params:nil success:^(id json) {
        if ([json[@"res"] integerValue] == 0) {
            MyModel * model = [[MyModel alloc] initWithDictionary:json[@"data"] error:nil];
            [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.hp_img_url] placeholderImage:[UIImage imageNamed:@"jiushi"]];
            cell.contentTextView.text = model.hp_content;
            cell.numLabel.text = model.hp_title;
            cell.authorLabel.text = model.hp_author;
            cell.dateLabel.text = model.hp_makettime;
        }else{
            [MBProgressHUD showError:@"好像有什么地方不对呢"];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络好像出错了呢"];
    }];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _sortArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(0, 0);
}

- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
